This notebook calculates the minimum and maximum elevation within a study area defined by a vecotor polygon boundary from a raster digital elevation model.
In [80]:
from geo.models import Raster
from geo.models import Boundary
import rasterio
from shapely.geometry import shape
import numpy
import numpy.ma
import rasterio.mask
from matplotlib import cm as colormaps
from matplotlib import pyplot
%matplotlib inline
In [3]:
dem_record = Raster.objects.get(name='dem')
In [45]:
boundary_record = Boundary.objects.get(name='study area')
boundary = [eval(boundary_record.geometry.geojson)]
In [104]:
with rasterio.open(path=dem_record.filepath, mode='r') as source:
dem, transform = rasterio.mask.mask(source, boundary, crop=True, nodata=-9999)
dem = numpy.ma.masked_equal(dem.data, -9999)
In [106]:
dem.min()
Out[106]:
In [107]:
dem.max()
Out[107]:
In [117]:
pyplot.imshow(dem[0], cmap='viridis', vmin=dem.min(), vmax=dem.max())
pyplot.tick_params(bottom=False, labelbottom=False,
left=False, labelleft=False,
top=False, right=False)
pyplot.axes().set_frame_on(False)
cb = pyplot.colorbar()